// const data = [
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"},
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"},
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"},
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"},
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"},
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"},
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"},
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"},
// 	{"image": "image url", "title": "title xxxx", "m3u8": "http://xxx.m3u8"}
// ]; // 这里替换成你的数据源

function gotoIndexPage() {
	p_table_type="website"

	let params = {}
	fetch_table_data("/website-list", params)
}

function gotoClassListPage(new_website_id) {

	p_table_type="class"

	let params = {}

	if (new_website_id) {
		p_website_id = new_website_id
	}

	if (p_website_id) {
		params["website_id"] = p_website_id
	}
	fetch_table_data("/class-list", params)
}

function gotoResourceListPage(new_class_id) {
	p_table_type = "resource"
	let params = {}

	if (new_class_id) {
		p_class_id = new_class_id
	}

	if (p_website_id) {
		params["website_id"] = p_website_id
	}
	if(p_class_id) {
		params["class_id"] = p_class_id
	}
	fetch_table_data("/resource-list", params)
}

function gotoSearchNameListPage() {
	let params = {}

	if (p_website_id) {
		params["website_id"] = p_website_id
	}

	if (p_search_name) {
		params["search_name"] = p_search_name
	}

	fetch_table_data("/search-name", params)
}

function updatePageHtml() {
	let table_name_ele = document.getElementById("table_name");
	table_name_ele.innerHTML = p_table_name;
	updateTableHtml(p_table_rows, p_table_cols)
	updatePageNum()
	goToPageNum(1)
}

function updateTableHtml(table_rows, table_cols) {
	table_html = ""

	// 表头
	table_html += "<thead><tr>"
	for (let i = 0; i < table_cols.length; i++) {
		table_html += "<th>"+table_cols[i][1]+"</th>"
	}
	table_html += "</tr></thead>"

	// 表身
	table_html += "<tbody>"
	for (let i = 0; i < table_rows.length; i++) {
		table_html += "<tr>"
		for (let j = 0; j < table_cols.length; j++) {
			table_html += "<td>"+renderCol(table_cols[j][0], table_rows[i])+"</td>"
		}
		table_html += "</tr>"
	}
	table_html += "</tbody>"

	let table = document.getElementById("table-1")
	console.log(table)
	console.log(table_html)
	table.innerHTML = table_html;
}

function renderCol(key, row_value) {

	console.log(p_table_type, key, row_value)
	let td_value = row_value[key]
	if (p_table_type === "website") {
		if (key === "url") {
			return "<a href='"+td_value+"'>"+td_value+"</a>"
		} else if(key === "id") {
			return "<button onclick='gotoClassListPage(\""+td_value+"\")'>进入网站</button> "
		}

	} else if (p_table_type === "class") {
		if(key === "id") {
			return "<button onclick='gotoResourceListPage(\""+td_value+"\")'>进入分类</button> "
		}
	} else if (p_table_type === "resource") {
		if(key === "id") {
			m3u8 = row_value["video"]
			return "<button onclick='copyText(\""+m3u8+"\")'>复制地址</button> "
		} else if (key === "class_id") {
			m3u8 = row_value["video"]
			name = row_value["name"]
			return "<button onclick='fetch_table_data(\"/download\", {\"m3u8\": \""+m3u8+"\", \"filename\": \""+name+"\"})'>下载视频</button> "
		} else if (key === "head_image") {
			td_value = "https://img0.baidu.com/it/u=2628748598,2753024369&fm=253&fmt=auto&app=120&f=JPEG?w=740&h=475"
			return "<img src=\""+td_value+"\" alt=\"图片\">"
		} else if (key === "video") {
			return "<video width='320' height='240' controls><source src='"+td_value+"' type='application/x-mpegURL'>浏览器不支持</video> "
		}
	} else if (p_table_type === "searchname") {

	}

	return row_value[key]
}

function updatePageNum() {
	let page = document.getElementById('current-page'); // 获
	page.innerHTML = "当前页数："+p_page_cur+"/"+p_page_count
}

function goToPageNum(page_num) {
	console.log("当前页数", page_num)
	if (page_num < 1) {
		return;
	} else if (page_num > p_page_count) {
		return;
	} else {

		p_page_cur = page_num
		// updatePage()
	}


}

function copyText(text) {
  navigator.clipboard.writeText(text).then(function() {
    console.log('复制成功！');
  }, function(err) {
    console.error('无法复制文本：', err);
  });
}
